package com.datastructure.test.stringtransform;

public class StringTransform {

    public static void main(String[] args) {
        StringTransform stringTransform = new StringTransform();
        String sample = stringTransform.trans("h i ", 4);
        System.out.println(sample);
    }

    public String trans(String s, int n) {
        // write code here
        StringBuilder stringBuilder = new StringBuilder();
        String[] split = s.split(" ",-1);
        int length = split.length;
        for (int i = 0; i < length; i++) {
            split[i]=transformString(split[i]);
        }
        for (int i = length-1; i >=0; i--) {
            if(i==0){
                stringBuilder.append(split[i]);
            } else {
                stringBuilder.append(split[i]);
                stringBuilder.append(" ");
            }
        }
        return stringBuilder.toString();
    }

    private String transformString(String s) {
        if(s.equals("")){
            return s;
        }
        String[] split = s.split("");
        StringBuilder stringBuilder = new StringBuilder();
        char temp;
        for (String value : split) {
            temp = value.charAt(0);
            if (temp >= 'a' && temp <= 'z') {
                temp -= 32;
                stringBuilder.append(temp);
            } else if (temp >= 'A' && temp <= 'Z') {
                temp += 32;
                stringBuilder.append(temp);
            }
        }
        return stringBuilder.toString();
    }
}
